package org.finmanager.statistics;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;

/**
 * Created by Kelvin on 28/4/2014.
 */

//A class to control the pie chart by getting categoryPieChartDataSet & categoryPieChartUI
public class CategoryPieChartController implements ActionListener {
	public static final String INCREASE_MONTH = "CategoryPieChartController.INCREASE_MONTH";
	public static final String DECREASE_MONTH = "CategoryPieChartController.DECREASE_MONTH";
	public static final String SET_CURRENT_MONTH = "CategoryPieChartController.SET_CURRENT_MONTH";

	private final CategoryPieChartDataSet categoryPieChartDataSet;
	private final CategoryPieChartUI categoryPieChartUI;

	public CategoryPieChartController(StatisticsController statisticsController) {
		this.categoryPieChartDataSet = statisticsController.getStatisticsModel().getCategoryPieChartDataSet();
		categoryPieChartUI = new CategoryPieChartUI(this);

		categoryPieChartUI.addButtonListener(this);

		statisticsController.addComponent(getUIMainComponent(), "Proportion - Category");
	}

	public CategoryPieChartDataSet getCategoryPieChartDataSet() {
		return categoryPieChartDataSet;
	}

	public JComponent getUIMainComponent() {
		return categoryPieChartUI.getMainComponent();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String ac = e.getActionCommand();

		if (ac.equals(INCREASE_MONTH)) {
			categoryPieChartDataSet.increaseMonth();
			categoryPieChartUI.setCurrentMonthText(categoryPieChartDataSet.getCalendar());
		}
		else if (ac.equals(DECREASE_MONTH)) {
			categoryPieChartDataSet.decreaseMonth();
			categoryPieChartUI.setCurrentMonthText(categoryPieChartDataSet.getCalendar());
		}
		else if (ac.equals(SET_CURRENT_MONTH)) {
			categoryPieChartDataSet.setCalendar(Calendar.getInstance());
			categoryPieChartUI.setCurrentMonthText(Calendar.getInstance());
		}
	}
}
